/**
 * Version 2.0 
 * Author: Rob Perez
 * Date: August 20, 2010
 * 
 * Usage:
 * 
 * 	Set a flag at the beginning of generateLeftNav to modify expansion behavior of the nav if desired.
 * 
 *  Somewhere prior to including the nav, set either noLeftNavExpansion or parentUrl var. 
 *  
  			<script type="text/javascript">
					
		-->					var noLeftNavExpansion = true; // really, this can be any value. We are just checking to see if it's defined at all. 
					
					In the above example, we don't want the left nav to expand. 
					Set that flag = left nav stays closed.
					
					OR
					
		-->						var parentUrl = "/ssl/ssl-information-center/";   // Trailing slash, please.
					
					In the above example, we are pretending the current page is a sub-category of the SSL Information Center.
					As such, the SSL Information Center category will be expanded and its children exposed.
					So, the current page will appear to "belong" under that sub-heading no matter where it actually lives.				
					
					Note that the nav will not expand at all if you just give it a "dummy" parentUrl that does not actually exist in the nav.
					However, you should use the noLeftNav expansion flag to accomplish this so your intent is clear to whomever shows up next to 
					modify this page.
					
				</script>
 * 
 * 
 * 
 */

generateLefNav = function(){
	generateLeftNav();
}

generateLeftNav = function(){

var hasParentUrl = false;	
var urlMatch = false;
var myLocation = "" + window.location;
var tempLocation = myLocation.split("?");
	
	if (typeof (noLeftNavExpansion) != 'undefined'){
		return false; // the no left nav expansion flag has been set. No expansion. We are done.
	}	
		
	if (typeof (parentUrl) != 'undefined'){
		parentUrl = buildFullPath(parentUrl);  // goes where you go, works where you work!
		hasParentUrl = true;  //there is a  parent url set. We'll attempt to expand the parent category of this page.
	}
	
	$("div#leftnav ul a").each(function(i){
		if (hasParentUrl) {
			if (this.href == parentUrl || this.href == parentUrl+"index.html") {
				$(this).parents("li:eq(0):not([class='main'])").addClass("activechild");
				return true; // a match to the  parent. Menu expanded.
			}
		}
		else
		myLocation = tempLocation[0];  // ignore any params when checking the url...
			if (this.href == myLocation || this.href + "index.html" == myLocation || stripIndex(this.href)== myLocation) {
				if ($(this).parents().length > 1) {
					$(this).parents("li:eq(1):not([class='main'])").addClass("activechild");
				}
				$(this).parents("li:eq(0):not([class='main'])").addClass("active");
				$(this).css("cursor", "text");
				$(this).css("textDecoration", "none");
				$(this).click(function(){
					return false;
				});
				urlMatch = true;
				return true; // a match with this page's URL; menu expanded. And...we're done.
			}
	});
	if(hasParentUrl){
		return false;  // if we've tried to match a parent and failed, pull the ripcord to avoid lots of unwanted recursion.
	}
	
	if(!urlMatch){ // if there's no match for the current location, try to derive and set a parentUrl and go once more.
		parentUrl = deriveParent();
		if(parentUrl){
			generateLeftNav();
		}
	}
}

/**
 *  	deriveParent() breaks up and re-assembles the current URL as it iterates through our ordered lists to find its parents.
 *  	In a nutshell, if it finds a parent that has children it has a match. BUT it continues to iterate until there are no more matches.
 *  	Then it returns the last match it found a.k.a. its closest parent with children a.k.a. the category to which it belongs.
 *  
 *  	Example: ssl -> ssl-information-center -> ssl-licencing or /ssl/ssl-information-center/ssl-licensing/index.html
 *  	
 *  	Will:
 *  	
 *  	1. Match SSL and set the "match" equal to that item
 *  	2. Keep iterating
 *  	3. Match SSL Information Center and set the "match" url equal to that, clobbering our first match
 *  	4. Keep iterating and find no further matches since ssl-licencing does not have children and is therefore not a "category" that we need to expand
 *  	5. Returns the last "match" var which is ssl-information-center
 *  	6. That gets fed back into generateLeftNav for proper expansion.  
 *  	7. The SSL Information Center category is expanded.
 *  
 */

deriveParent = function(){
	var match = false;
	pathArray = window.location.pathname.split( '/' );
	newPathname = "";
	for ( i = 0; i < pathArray.length; i++ ) {
		if (i != 0) {
			newPathname += "/";
		}
		newPathname += pathArray[i];
		fullPathname = buildFullPath(newPathname);
		$("div#leftnav ul a").each(function(i){			
			if ((this.href == fullPathname || this.href == fullPathname+"/index.html")&&$(this).parent().siblings("li").find("ul").length>0){
				match = newPathname + "/";
			}	
		});
	}		
	return match;
}

buildFullPath = function(myPathName){
	return window.location.protocol + "//" + window.location.host + myPathName; 
}

stripIndex = function(myUrl){
	return myUrl.replace(/index.html/i, "");
}
